#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
#include <numeric>

bool canDivide(std::vector<int> &times, int days, int maxT)
{
    int curr_sum = 0;
    int day_cnt = 1;
    int max_task = 0;
    bool is_skip = false;

    for (int i = 0; i < times.size(); i++)
    {
        int t = times[i];
        max_task = std::max(max_task, t);
        if (curr_sum + t <= maxT)
        {
            curr_sum += t;
        }
        else if (!is_skip)
        {
            curr_sum -= max_task;
            curr_sum += t;
            is_skip = true;
        }
        else
        {
            is_skip = false;
            curr_sum = 0;
            max_task = 0;
            day_cnt++;
            i--;
            if (day_cnt > days)
            {
                return false;
            }
        }
    }

    return true;
}

int main()
{
    std::string s;
    std::getline(std::cin, s);
    std::istringstream iss(s);

    std::vector<int> times;
    std::string tok;
    while (std::getline(iss, tok, ','))
    {
        times.push_back(std::stoi(tok));
    }

    int M;
    std::cin >> M;

    if (M >= times.size())
    {
        std::cout << 0 << std::endl;
    }
    else
    {
        int l = 0;
        int r = std::accumulate(times.begin(), times.end(), 0);
        while (l < r)
        {
            int m = (l + r) / 2;
            if (canDivide(times, M, m))
            {
                r = m;
            }
            else
            {
                l = m + 1;
            }
        }
        std::cout << l << std::endl;
    }

    return 0;
}